<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Controller_User extends Controller_Template {
	protected $_template = 'other/master';

	/**
	 * List of documents
	 */
	public function indexAction($group_id = -1) {
		if ($group_id > -1) {
			Session::set('user_group_id', intval($group_id));
		} else if (Session::is('user_group_id')) {
			$group_id = intval(Session::get('user_group_id'));
		} else {
			$group_id = 0;
		}
		
		$this->template->content = View::factory('user/index')
			->set('group_id', $group_id)
			->set('group_list', Group::getList())
			->set('user_list', User::getList($group_id))
			->render();
	}

	/**
	 * Insert user
	 */
	public function insertAction($error = -1) {
		if (Input::isPostBack()) {
			$data = array();
			Query::data($data, 'name', Input::post('user_name'), Query::DATA_STRING);
			Query::data($data, 'login', Input::post('user_login'), Query::DATA_STRING);
			Query::data($data, 'password', md5(Input::post('user_password')), Query::DATA_STRING);
			Query::data($data, 'utc_offset', Input::post('user_utc_offset'), Query::DATA_INTEGER);
			Query::data($data, 'status_id', Input::post('user_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'insert_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'insert_time', time(), Query::DATA_INTEGER);

			$dst_offset = Input::post('user_dst_offset');
			Query::data($data, 'dst_offset', (!empty($dst_offset) ? 3600 : 0), Query::DATA_INTEGER);

			$new_user_id = User::insert($data);
			
			if ($new_user_id) {
				User::addToGroup($new_user_id, Input::post('user_group_id'));
				Tools::redirect(BASE_URL . 'user/update/' . $new_user_id . '/-2');
			} else {
				Tools::redirect(CURRENT_URL . '1');
			}
		} else {
			$group_id = 0;
			if (Session::is('user_group_id')) {
				$group_id = intval(Session::get('user_group_id'));
			}
			
			$this->template->content = View::factory('user/insert')
				->set('error', $error)
				->set('group_id', $group_id)
				->set('group_list', Group::getList())
				->render();
		}
	}

	/**
	 * Update user
	 */
	public function updateAction($id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$data = array();
			Query::data($data, 'name', Input::post('user_name'), Query::DATA_STRING);
			Query::data($data, 'login', Input::post('user_login'), Query::DATA_STRING);
			Query::data($data, 'utc_offset', Input::post('user_utc_offset'), Query::DATA_INTEGER);
			Query::data($data, 'status_id', Input::post('user_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'update_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'update_time', time(), Query::DATA_INTEGER);

			$dst_offset = Input::post('user_dst_offset');
			Query::data($data, 'dst_offset', (!empty($dst_offset) ? 3600 : 0), Query::DATA_INTEGER);

			$password = Input::post('user_password');
			if (!empty($password)) {
				Query::data($data, 'password', md5($password), Query::DATA_STRING);
			}
			
			if (User::update($id, $data)) {
				Tools::redirect(CURRENT_URL . $id . '/0');
			} else {
				Tools::redirect(CURRENT_URL . $id . '/1');
			}
		} else {
			$this->template->content = View::factory('user/update')
				->set('error', $error)
				->set('info', User::getInfo($id))
				->render();
		}
	}

	/**
	 * Delete user
	 */
	public function deleteAction($user_id, $group_id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$delete_from = intval(Input::post('delete_from'));
			$delete_code = 0;
			if (1 == $delete_from) {
				$data = array();
				Query::data($data, 'status_id', 3, Query::DATA_INTEGER);
				Query::data($data, 'delete_user_id', User::getId(), Query::DATA_INTEGER);
				Query::data($data, 'delete_time', time(), Query::DATA_INTEGER);

				if (!User::delete($user_id, $data)) {
					$delete_code = 1;
				}
			} else if (2 == $delete_from) {
				$group_list = Input::post('delete_from_groups');
				foreach ($group_list as $group) {
					User::removeFromGroup($user_id, $group);
				}
			}
			
			Tools::redirect(CURRENT_URL . $user_id . '/' . $group_id . '/' . $delete_code);
		} else {
			$in_groups = User::inGroups($user_id);
			$group_list = array();
			foreach ($in_groups as $group) {
				$group_list[] = Group::getInfo($group, 'id,name');
			}

			$this->template->content = View::factory('user/delete')
				->set('error', $error)
				->set('group_id', $group_id)
				->set('info', User::getInfo($user_id, 'id,name'))
				->set('group_list',  $group_list)
				->render();
		}
	}

	/**
	 * Groups
	 */
	public function groupsAction($user_id, $group_id = 0, $action = 0) {
		$error = -1;
		if (($group_id > 0) && ($action > 0)) {
			$result = false;

			if (1 == $action) {
				$result = User::addToGroup($user_id, $group_id);
			} else if (2 == $action) {
				$result = User::removeFromGroup($user_id, $group_id);
			}

			if (!$result) {
				$error = 1;
			}
		}

		$this->template->content = View::factory('user/groups')
			->set('error', $error)
			->set('info', User::getInfo($user_id))
			->set('group_id', $group_id)
			->set('group_list',  Group::getList())
			->set('in_groups',  User::inGroups($user_id))
			->render();
	}

	/**
	 * Login
	 */
	public function loginAction($error = 0) {
		if (Input::isPostBack()) {
			$error_code = 0;
			$user_id = User::checkLogin(
				Input::post('login'),
				Input::post('password')
			);
			if (!$user_id) {
				$error_code = 1;
			}

			if (!$error_code && !User::checkAccess($user_id)) {
				$error_code = 2;
			}

			Session::set('user_login', Input::post('login'));

			if (!$error_code) {
				Auth::login($user_id);
				Tools::redirect(BASE_URL);
			} else {
				Tools::redirect(BASE_URL . 'user/login/' . $error_code);
			}
		} else {
			$error_description = '';
			if ($error > 0) {
				if (1 == $error) {
					$error_description = __('Wrong user name or/and password');
				} else if (2 == $error) {
					$error_description = __('Access denied');
				} else {
					$error_description = __('Unspecified error');
				}
			}
			$login = '';
			if (Session::is('user_login')) {
				$login = Session::get('user_login');
			}
			
			$this->template = null;
			$view = View::factory('user/login')
				->set('login', $login)
				->set('error', $error_description);

			Output::body($view->render());
		}
	}

	/**
	 * Logout
	 */
	public function logoutAction() {
		Auth::logout();
		Tools::redirect(BASE_URL);
	}
}